package com.ouheng.shop.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ouheng.shop.dao.ProductDao;
import com.ouheng.shop.dao.ProductPriceDao;
import com.ouheng.shop.domain.ProductDO;
import com.ouheng.shop.domain.ProductPriceDO;
import com.ouheng.shop.service.ProductPriceService;



@Service
public class ProductPriceServiceImpl implements ProductPriceService {
	@Autowired
	private ProductPriceDao productPriceDao;
	
	@Autowired
	private ProductDao productDao;
	
	@Override
	public ProductPriceDO get(Long id){
		return productPriceDao.get(id);
	}
	
	@Override
	public List<ProductPriceDO> list(Map<String, Object> map){
		return productPriceDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return productPriceDao.count(map);
	}
	
	@Override
	@Transactional
	public int save(ProductPriceDO productPrice){
		List<ProductPriceDO> listProductPrice = productPriceDao.list(null);
		if(listProductPrice.isEmpty()){
			productPrice.setStatus("2");
		}else{
			if("2".equals(productPrice.getStatus())){
				Map<String, Object> map = new HashMap<>();
				map.put("pid", productPrice.getPid());
				map.put("status", 2);
				List<ProductPriceDO> ProductPriceDO = productPriceDao.list(map);
				for (ProductPriceDO productPriceDO2 : ProductPriceDO) {
					productPriceDO2.setStatus("1");
					productPriceDao.update(productPriceDO2);
				}
			}
		}
		int count = productPriceDao.save(productPrice);
		if("2".equals(productPrice.getStatus())){
			ProductDO productDO = productDao.get(productPrice.getPid());
			productDO.setPpid(productPrice.getId());
			productDO.setPrice(productPrice.getPrice());
			productDO.setPromoteprice(productPrice.getPromoteprice());
			productDao.update(productDO);
		}
		return count;
	}
	
	@Override
	@Transactional
	public int update(ProductPriceDO productPrice){
		if("2".equals(productPrice.getStatus())){
			Map<String, Object> map = new HashMap<>();
			map.put("pid", productPrice.getPid());
			map.put("status", 2);
			List<ProductPriceDO> list = productPriceDao.list(map);
			if(!list.isEmpty()){
				for (ProductPriceDO productPriceDO : list) {
					productPriceDO.setStatus("1");
					productPriceDao.update(productPriceDO);
				}
			}
		}
		int count  = productPriceDao.update(productPrice);
		if("2".equals(productPrice.getStatus())){
			ProductDO productDO = productDao.get(productPrice.getPid());
			productDO.setPpid(productPrice.getId());
			productDO.setPrice(productPrice.getPrice());
			productDO.setPromoteprice(productPrice.getPromoteprice());
			productDao.update(productDO);
		}
		return count;
	}
	
	@Override
	@Transactional
	public int remove(Long id){
		return productPriceDao.remove(id);
	}
	
	@Override
	@Transactional
	public int batchRemove(Long[] ids){
		return productPriceDao.batchRemove(ids);
	}
	
}
